package com.wjjun.library.widget.dropdown;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.BindingAdapter;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.InverseBindingListener;
import androidx.databinding.InverseBindingMethod;
import androidx.databinding.InverseBindingMethods;
import androidx.databinding.ViewDataBinding;

import com.wjjun.base.BR;
import com.wjjun.base.ui.helper.RecycleViewHelper;
import com.wjjun.base.ui.listener.IItemViewListener;
import com.wjjun.base.ui.viewmodel.BaseItemViewModel;
import com.wjjun.library.viewmodel.CheckItemViewModel;

import java.util.ArrayList;
import java.util.List;

/**
 * 基础的下拉弹窗tab控件
 * author : wangjunjun
 * date   : 2021/10/21
 * desc   :
 */
@InverseBindingMethods({
        @InverseBindingMethod(
                type = BaseListMenuTabView.class,
                attribute = "selectList",
                event = "selectListAttrChanged",
                method = "getSelectList")
})
public abstract class BaseListMenuTabView extends BaseMenuTabView implements IItemViewListener {

    protected RecycleViewHelper itemListViewProxy;

    //是否选中后替换标题为选中的item名称
    protected boolean titleIsShowSelectItem = false;
    protected String text;

    protected List<? extends BaseItemViewModel<?>> selectList = new ArrayList<>();
    protected List<? extends BaseItemViewModel<?>> data = new ArrayList<>();

    protected OnSelectChange onSelectChange;


    public BaseListMenuTabView(@NonNull Context context) {
        this(context, null);
    }

    public BaseListMenuTabView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BaseListMenuTabView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void init() {
        super.init();
        text = getText().toString();
    }

    protected View initContentView() {
        ViewDataBinding viewDataBinding = DataBindingUtil.inflate(LayoutInflater.from(getContext()),
                getLayoutId(), null, false);
        viewDataBinding.setVariable(BR.listener, this);
        initList(viewDataBinding);
        return viewDataBinding.getRoot();
    }

    protected abstract @LayoutRes
    int getLayoutId();

    protected void initList(ViewDataBinding viewDataBinding) {
        itemListViewProxy = new RecycleViewHelper();
        itemListViewProxy.setOnItemClickListener(this);
        viewDataBinding.setVariable(BR.viewModel, itemListViewProxy.buildViewModel());

        if (data != null) {
            if (itemListViewProxy != null) {
                itemListViewProxy.clearData();
                itemListViewProxy.addData(data);
            }
        }
    }

    public void setData(List<? extends BaseItemViewModel<?>> list) {
        if (data != null) {
            data.clear();
        }
        data = list;
        if (itemListViewProxy != null) {
            itemListViewProxy.clearData();
            itemListViewProxy.addData(list);
        }
    }

    @Override
    public void onChange(boolean isShow) {
        super.onChange(isShow);
        if (isShow) {
            update();
        } else if (isTitleIsShowSelectItem()) {
            if (selectList != null && !selectList.isEmpty()) {
                StringBuilder stringBuilder = new StringBuilder();
                for (BaseItemViewModel viewModel : selectList) {
                    if (viewModel instanceof CheckItemViewModel) {
                        if (((CheckItemViewModel) viewModel).isDef()) {
                            continue;
                        }
                        if (stringBuilder.length() > 0) {
                            stringBuilder.append(",");
                        }
                        stringBuilder.append(((CheckItemViewModel) viewModel).getText().getValue());
                    }
                }
                if (stringBuilder.length() > 0) {
                    setText(stringBuilder.toString());
                    return;
                }
            }
            setText(text);
        }
    }

    protected void update() {
        for (BaseItemViewModel item : itemListViewProxy.getAllData()) {
            if (item instanceof CheckItemViewModel) {
                CheckItemViewModel itemViewModel = (CheckItemViewModel) item;
                if (selectList == null || selectList.isEmpty()) {
                    itemViewModel.getChecked().setValue(itemViewModel.isDef());
                } else {
                    itemViewModel.getChecked().setValue(selectList.contains(item));
                }
            }
        }
    }

    public boolean isTitleIsShowSelectItem() {
        return titleIsShowSelectItem;
    }

    public void setTitleIsShowSelectItem(boolean titleIsShowSelectItem) {
        this.titleIsShowSelectItem = titleIsShowSelectItem;
    }

    public List<? extends BaseItemViewModel<?>> getSelectList() {
        return selectList;
    }

    public void setSelectList(List<? extends BaseItemViewModel<?>> selectList) {
        if (selectList == null) {
            return;
        }
        this.selectList = selectList;
    }

    protected void onConfirm() {
        if (onSelectChange != null) {
            onSelectChange.onChange(selectList);
        }
        if (onDropDownListener != null) {
            onDropDownListener.onConfirm(this);
        }
        dropDownView.closeMenu();
    }

    @BindingAdapter("selectListAttrChanged")
    public static void setChangeListener(BaseListMenuTabView view, final InverseBindingListener bindingListener) {
        if (bindingListener != null) {
            // 通知 ViewModel
            view.setOnSelectChange(new OnSelectChange() {
                @Override
                public void onChange(List<? extends BaseItemViewModel<?>> list) {
                    bindingListener.onChange();
                }
            });
        }
    }

    public void setOnSelectChange(OnSelectChange onSelectChange) {
        this.onSelectChange = onSelectChange;
    }

    public interface OnSelectChange {
        void onChange(List<? extends BaseItemViewModel<?>> list);
    }
}
